# Django settings for powerdb project.

# SETTINGS FOR EVERYTHING
# SDH : README HERE
# you should be able to do a 'python manage.py runserver' to
# start the development server

import os
import sys

try:
    PROJECT_ROOT
except NameError:
    PROJECT_ROOT = os.path.dirname(__file__)
    sys.path.append(os.path.join(PROJECT_ROOT, 'lib'))

DEBUG = True
USE_TZ = True
TEMPLATE_DEBUG = DEBUG

ADMINS = (
     ('David Heinzerling', 'dheinzerling@gmail.com'),
)

MANAGERS = ADMINS

DATABASES = {
    'default': {
        'ENGINE': 'django.contrib.gis.db.backends.postgis',
        'NAME': 'toolkit',
        'USER': 'cbe',
        'PASSWORD': 'G3ccFoUw',
    }
}

ADMIN_BACKEND = "http://localhost:8070/"

# the ARchiver Daemon is a process which manages the archive of your
# time-series data, and tags.  The Berkeley ARD runs at the location
# below; you can use it if you have an API key; even without it, you
# can query public streams.

# ARD_URL = 'http://smote.cs.berkeley.edu:8079'
ARD_URL = 'http://new.openbms.org/backend'


# if you have an API key, put it here.  this will allow you to access
# streams whose data is associated with your key in the backend and
# are marked as private.
# ARD_KEY= 'LdJVVhKssRAnbkvAugbbCZ0HPeSZPrVDb0uW'
ARD_KEY = ('SA2nYWuHrJxmPNK96pdLKhnSSYQSPdALkvnA', 'WE4iJWG7k575AluJ9RJyAZs25UO72Xu0b4RA')

# if this is True, then the backend will *only* show you streams which
# are "yours" -- that is, assoicated with your API key.  Otherwise,
# you will also query all "public" streams.
ARD_PRIVATE = False

# Local time zone for this installation. Choices can be found here:
# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
# although not all choices may be available on all operating systems.
# If running in a Windows environment this must be set to the same as your
# system time zone.
TIME_ZONE = 'America/Los_Angeles'

# Language code for this installation. All choices can be found here:
# http://www.i18nguy.com/unicode/language-identifiers.html
LANGUAGE_CODE = 'en-us'

SITE_ID = 1

# If you set this to False, Django will make some optimizations so as not
# to load the internationalization machinery.
USE_I18N = True

# Absolute path to the directory that holds media.
# Example: "/home/media/media.lawrence.com/"
MEDIA_ROOT = '/opt/bpe_toolkit/staticStore/userData/'

# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash if there is a path component (optional in other cases).
# Examples: "http://media.lawrence.com", "http://example.com/media/"
MEDIA_URL = '/media/'

LOGIN_URL = '/admin/login/'

LOGIN_REDIRECT_URL = '/pmp/'

STATIC_URL = '/static/'
STATIC_ROOT = '/opt/bpe_toolkit/staticStore/'

#comment next two lines and uncomment previous line when moving this to production (not sure why this doesn't work properly in development version)
#STATIC_ROOT = ''
#STATICFILES_DIRS = ( '/opt/bpe_toolkit-dev/bpe_toolkit/staticStore/', )

# Make this unique, and don't share it with anybody.
SECRET_KEY = 'cit%=^y%q)2r*!ek0*__j9e7l+$sfd#30gythi$g79ip2-cywv'

# List of callables that know how to import templates from various sources.
TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader',
    'django.template.loaders.eggs.Loader',
)

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
)

ROOT_NETLOC = 'new.openbms.org'
ROOT_URLCONF = 'bpe_toolkit.urls'

TEMPLATE_DIRS = (
    # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
    # '/mnt/md1/django/powerdb/smap/',
    os.path.join(PROJECT_ROOT, 'templates/'),
)

TEMPLATE_CONTEXT_PROCESSORS = ("django.contrib.auth.context_processors.auth",
"django.core.context_processors.debug",
"django.core.context_processors.i18n",
"django.core.context_processors.media",
"django.core.context_processors.static",
"django.core.context_processors.tz",
"django.contrib.messages.context_processors.messages")

STATIC_DOC_ROOT = os.path.join(PROJECT_ROOT, 'media/')
PMP_DOC_ROOT = os.path.join(PROJECT_ROOT, 'pmp/media/')

INSTALLED_APPS = (
    'bpe_toolkit.pmp',
    #'bpe_toolkit.smap',
    'django.contrib.auth',
    'django.contrib.sites',
    'django.contrib.contenttypes',
    'django.contrib.messages',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.admin',
    'django.contrib.staticfiles',
    'django.contrib.gis',
    'widget_tweaks',
    'revproxy',
    'south',
    'olwidget'
)

OLWIDGET_DEFAULT_OPTIONS = {
    'layers': ['osm.mapnik'],
}

OLWIDGET_CUSTOM_LAYER_TYPES = {
    'image_layer': """OpenLayers.Layer.TMS(
        'Image TMS',
        '',
        {url: '',
        serviceVersion: '.',
        layername: '.',
        alpha: true,
        type: 'png',
        getURL: overlay_getTileURL 
        }
    )
       function overlay_getTileURL(bounds) {
            var projectId = sessvars.sessionObj['projectId']
            floor = $('#id_floor').val();
            var res = this.map.getResolution();
            var x = Math.round((bounds.left - this.maxExtent.left) / (res * this.tileSize.w));
            var y = Math.round((bounds.bottom - this.maxExtent.bottom) / (res * this.tileSize.h));
            var z = this.map.getZoom();
            if (x >= 0 && y >= 0) {
                return "/static/userData/" + projectId + "/" + floor + "/" + z + "/" + x + "/" + y + "." + this.type;                
            } else {
                return "http://www.maptiler.org/img/none.png";
            }
        }
    """
}
